<template>
  <div class="login-container">
    <el-row>
      <el-col :xs="11" :sm="11" :md="12" :lg="14">
        <div class="grid-content"></div>
      </el-col>
      <el-col :xs="12" :sm="11" :md="10" :lg="6">
        <div class="grid-content bg-purple-light">
          <el-form autoComplete="on" :model="loginForm" :rules="loginRules" ref="loginForm" label-position="left" label-width="0px" class="card-box login-form">
            <h3 class="title"><img src="../../assets/login/logo.png" />农产品追溯管理平台</h3>
            <el-form-item prop="username">
              <span class="svg-container">
                <icon-svg icon-class="terminal"></icon-svg>
              </span>
              <el-input name="username" type="text" v-model="loginForm.username" autoComplete="on" placeholder="手机号" :maxlength="11"></el-input>
            </el-form-item>
            <el-form-item prop="userpassword">
              <span class="svg-container">
                <icon-svg icon-class="mima1"></icon-svg>
              </span>
              <el-input name="userpassword" type="password" @keyup.enter.native="handleLogin" v-model="loginForm.userpassword" autoComplete="off" placeholder="密码" :maxlength="12"></el-input>
            </el-form-item>
            <el-form-item>
              <el-button class="loginBtn" :loading="loading" @click.native.prevent="handleLogin">
                登录
              </el-button>
            </el-form-item>
            <div class="tips">{{tips}}</div>
          </el-form>
        </div>
      </el-col>
      <el-col :xs="1" :sm="2" :md="2" :lg="4">
        <div class="grid-content"></div>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import { isWsTelno, isWsPassword } from 'utils/validate';
import Cookies from 'js-cookie';

const operatorid = Cookies.get('uid');
export default {
  name: 'login',
  data() {
    const validateTelNo = (rule, value, callback) => {
      if (!isWsTelno(value)) {
        callback(new Error('请输入正确的手机号'));
      } else {
        callback();
      }
    };
    return {
      loginForm: {
        username: '',
        userpassword: '',
        plattype: 'role_admin'
      },
      loginRules: {
        username: [
          { required: true, trigger: 'blur', validator: validateTelNo },
          { min: 0, max: 11, message: '输入最多允许11个字符', trigger: 'blur' }
        ],
        userpassword: [{ required: true, message: '请输入密码', trigger: 'blur' },
        { min: 0, max: 12, message: '输入最多允许12个字符', trigger: 'blur' }],
      },
      tips: '',
      loading: false,
      showDialog: false
    }
  },
  methods: {
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true;
          this.$store.dispatch('LoginByEmail', this.loginForm).then((res) => {
            this.loading = false;
            this.$router.push({ path: '/' });
            this.tips = res || '';
          }).catch(err => {
            this.$message.error(err);
            this.loading = false;
          });
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    }
  },
  created() {
    // window.addEventListener('hashchange', this.afterQRScan);
    // this.tips = (this.tips === '')?(Cookies.get('lastoptime')?'长时间未操作，请重新登录':''):''
  },
  destroyed() {
    // window.removeEventListener('hashchange', this.afterQRScan);
  }
}
</script>

<style rel="stylesheet/scss" lang="scss">
@import "src/styles/mixin.scss";
.tips {
  font-size: 14px;
  color: red;
  margin-bottom: 5px;
  font-family: Helvetica Neue, Helvetica, PingFang SC, Hiragino Sans GB, Microsoft YaHei, Arial, sans-serif;
}

.login-container {
  @include relative;
  height: 100vh; // background-image: url("../../assets/login/bimg.png");
  background-color: #2d3a4b;
  input:-webkit-autofill {
    -webkit-box-shadow: 0 0 0px 1000px #293444 inset !important;
    -webkit-text-fill-color: #fff !important;
  }
  input {
    background: transparent;
    border: 0px;
    -webkit-appearance: none;
    border-radius: 0px;
    padding: 12px 5px 12px 15px;
    color: #eeeeee;
    height: 47px;
  }
  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;
  }
  .svg-container {
    padding: 6px 5px 6px 15px;
    color: #889aa4;
  }
  .title {
    font-size: 23px;
    font-weight: 400;
    color: #eeeeee;
    margin: 0px auto 20px auto;
    text-align: center;
    font-weight: bold;
    vertical-align: middle;
  }

  .title img {
    vertical-align: middle;
    width: 48px;
    height: 48px;
    margin-right: 10px;
  }

  .login-form {
    position: absolute;
    left: 0;
    right: 0;
    width: 400px;
    padding: 35px 35px 15px 35px;
    margin: 120px auto;
  }
  .el-form-item {
    border: 1px solid rgba(255, 255, 255, 0.1);
    background: rgba(0, 0, 0, 0.1);
    border-radius: 5px;
    color: #454545;
  }
  .forget-pwd {
    color: #fff;
  }

  .loginBtn {
    width: 100%;
    font-size: 20px;
  }
}
</style>
